package com.dc.bobaoge.project.v2.to.v3.business.core.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfo;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoMapper;
import com.dc.bobaoge.project.base.core.constant.AuthInfoConstant;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerPayDeposit;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerPayDepositMapper;
import com.own.business.system.dictionary.adapter.util.DictionaryUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * CerCommonUtil
 *
 * @author chenxueli
 * @date 2024-01-31 19:01:00
 */
@Slf4j
@Component
public class CerCommonUtil {

    public static final Map<Integer, Integer> STATUS_MAP = new HashMap<>() {{
        // 修改资料
        put(AuthInfoConstant.OldStatus.PERFECT_INFO, AuthInfoConstant.NewUpdateStatus.WAIT);
        put(AuthInfoConstant.OldStatus.MODIFY_INFO, AuthInfoConstant.NewUpdateStatus.WAIT);
        put(AuthInfoConstant.OldStatus.PERFECT_INFO_REJECT, AuthInfoConstant.NewUpdateStatus.REJECT);
        put(AuthInfoConstant.OldStatus.MODIFY_INFO_REJECT, AuthInfoConstant.NewUpdateStatus.REJECT);

        put(AuthInfoConstant.OldStatus.PASS, AuthInfoConstant.NewStatus.PASS);
        put(AuthInfoConstant.OldStatus.REJECT_SUBMIT_AGAIN, AuthInfoConstant.NewStatus.WAIT);

        // 添加历史
        put(AuthInfoConstant.OldStatus.AUDITING, AuthInfoConstant.NewStatus.WAIT);
        put(AuthInfoConstant.OldStatus.RENEWED, AuthInfoConstant.NewStatus.WAIT);
        put(AuthInfoConstant.OldStatus.UPGRADE, AuthInfoConstant.NewStatus.WAIT);
        put(AuthInfoConstant.OldStatus.RENEWED_REJECT, AuthInfoConstant.NewStatus.REJECT);
        put(AuthInfoConstant.OldStatus.UPGRADE_REJECT, AuthInfoConstant.NewStatus.REJECT);
        put(AuthInfoConstant.OldStatus.REJECT, AuthInfoConstant.NewStatus.REJECT);
    }};

    public static final Map<String, Integer> CER_COMPANY_IDENTICAL_MAP = new HashMap<>() {{
        put("1", 1);
        put("2", 0);
    }};

    @Resource
    private DictionaryUtil dictionaryUtil;
    @Resource
    private AuthenticationInfoMapper authenticationInfoMapper;
    @Resource
    private OldCoreCerPayDepositMapper oldCoreCerPayDepositMapper;

    /**
     * 根据经营类型名称获取经营类型id
     *
     * @param name 经营类型名称
     * @return 经营类型id
     */
    public Long getBusinessTypeIdByName(String name) {
        return dictionaryUtil.getIdByNameAndRootCode(resolveBusinessTypeName(name), "business_type");
    }

    /**
     * 处理经营范围的内容
     *
     * @param name 经营范围名称
     * @return 经营范围名称
     */
    public String resolveBusinessTypeName(String name) {
        if (name.contains("、")) {
            // 取后半部分
            name = name.substring(name.indexOf("、") + 1);
        }
        return name;
    }

    /**
     * 根据用户id获取认证信息
     *
     * @param userId 用户id
     * @return 认证信息
     */
    public AuthenticationInfo getAuthInfoByUserId(Long userId) {
        var wrapper = new LambdaQueryWrapper<AuthenticationInfo>()
                .eq(AuthenticationInfo::getFkUserId, userId);
        return authenticationInfoMapper.selectOne(wrapper);
    }

    /**
     * 根据用户id获取押金信息
     *
     * @param userId 用户id
     * @return 押金信息
     */
    public OldCoreCerPayDeposit getOldCoreCerPayDepositByMsgId(Long userId) {
        var wrapper = new LambdaQueryWrapper<OldCoreCerPayDeposit>()
                .eq(OldCoreCerPayDeposit::getCerUserId, userId)
                .orderByDesc(OldCoreCerPayDeposit::getCerPayDate)
                .last("limit 1");
        return oldCoreCerPayDepositMapper.selectOne(wrapper);
    }

}
